﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;


namespace University.Workshop.Presenters.WaterReadingControl.Modify
{
    public class PresenterModifyWaterReading
    {
        IModifyWaterReadingView view;
        Model.WaterReadingControl waterReading;
        public PresenterModifyWaterReading(IModifyWaterReadingView view, Model.WaterReadingControl waterReading)
        {
            this.view = view;
            this.waterReading= waterReading;
        }

        public void UpdateViewData()
        {
            view.Allotment = waterReading.Kardex.Allotment.First().AllotmentName;
            view.Square = waterReading.Kardex.Allotment.First().SquareId;
            view.Name = waterReading.Kardex.Person.Names;
            view.LastName = waterReading.Kardex.Person.LastNames;
            view.PreviousReading = waterReading.PreviousMeasurerRead;
            view.CurrentReading = waterReading.CurrentMeasurerRead;
            view.PaymentAmount = waterReading.PaidAmount;
        }

        public void CalculatePaymentAmount()
        {
            //decimal waterFare = waterReading.WaterFare;
            //double currentRead = view.CurrentReading;
            //double previousRead = waterReading.PreviousMeasurerRead;
            //if (currentRead == 0)
            //{
            //    view.NotifyMessage("Debe llenarse la lectura actual");
            //    return;
            //}

            //decimal result = ((decimal)(currentRead - previousRead)) * waterFare;
            //if(result)
            //result = Math.Round(result, 1);
            //view.PaymentAmount = result;
            try
            {
                decimal result = ManagerWaterReadingControl.CalculatePaymentAmount(waterReading, view.CurrentReading);
                view.PaymentAmount = result;
            }
            catch (Exception ex)
            {
                view.NotifyMessage(ex.Message);
            }
        }
        
        public void ModifyWaterReading()
        {
            try
            {
                waterReading.CurrentMeasurerRead = view.CurrentReading;
                waterReading.PaidAmount = view.PaymentAmount;
                
                ManagerWaterReadingControl.UpdateWaterReading(waterReading);
                view.ReturnSearchWaterReading();
            }
            catch (Exception ex)
            {
                view.NotifyMessage(ex.Message);
            }
        }
    }
}
